/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.market;

import cz.fi.muni.diplomka.common.enumeration.MovementType;
import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.dto.ShopOffer;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityResourcesManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.MarketManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityResourcesManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.MarketManagerImpl;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class BuyPage extends WebPage {

    private MarketManager marketManager = new MarketManagerImpl();
    private CityResourcesManager cityResourcesManager = new CityResourcesManagerImpl();
    private CityManager cityManager = new CityManagerImpl();
    private String selectedCity = "";
    private List<String> cities;

    public BuyPage(PageParameters parameters) {

        final Long id;

        add(new FeedbackPanel("feedback"));

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));

            PageParameters p = new PageParameters();
            p.add("id", id.toString());

            add(new LinkPanel("linkPanel", p));


            cities = new ArrayList<>();
            for (City c : cityManager.getCitiesByPlayerId(id)) {
                cities.add(c.getName());
            }
            if (parameters.containsKey("cityName")) {
                selectedCity = parameters.getString("cityName");
            }

            if (selectedCity.equals("")) {
                selectedCity = cities.get(0).toString();
            }

            p.add("cityName", selectedCity);

            add(new MarketPanel("marketPanel", p));

            if (parameters.containsKey("shopOfferId")) {
                Long shopOfferId = parameters.getLong("shopOfferId");
                ShopOffer shopOffer = marketManager.getShopOfferById(shopOfferId);

                if (cityResourcesManager.getResourceCount(cityManager.getCityByName(selectedCity).getId(), shopOffer.getWantedResource()) < shopOffer.getWantedResourceCount()) {

                    error("Not enough of resource in city to buy.");
                } else {
                    marketManager.deleteShopOffer(parameters.getLong("shopOfferId"));

                    marketManager.sendResources(shopOffer.getCityId(), cityManager.getCityByName(selectedCity).getId(), shopOffer.getSoldResource(), shopOffer.getSoldResourceCount(), MovementType.Shop);
                    marketManager.sendResources(cityManager.getCityByName(selectedCity).getId(), shopOffer.getCityId(), shopOffer.getWantedResource(), shopOffer.getWantedResourceCount(), MovementType.Shop);
                    info("Shop offer has been bought.");
                }
            }

            final DropDownChoice<String> city = new DropDownChoice<>("city",
                    new PropertyModel<String>(this, "selectedCity"), cities);

            Form<?> selectCity = new Form<Void>("selectCity") {
                @Override
                protected void onSubmit() {
                    selectedCity = cities.get(Integer.parseInt(city.getValue()));
                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", id.toString());
                    pageParameters.add("cityName", selectedCity);
                    setResponsePage(BuyPage.class, pageParameters);
                }
            };

            add(selectCity);
            selectCity.add(city);

            List<ShopOffer> list = marketManager.getShopOffersNotOfPlayer(id);

            ListView listview = new ListView("listview", list) {
                @Override
                protected void populateItem(ListItem item) {
                    ShopOffer shopOffer = (ShopOffer) item.getModelObject();
                    item.add(new Label("wanted", shopOffer.getWantedResource().toString() + ": " + shopOffer.getWantedResourceCount()));
                    item.add(new Label("offered", shopOffer.getSoldResource().toString() + ": " + shopOffer.getSoldResourceCount()));
                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", id.toString());
                    pageParameters.add("shopOfferId", shopOffer.getId().toString());
                    pageParameters.add("type", "recieved");
                    BookmarkablePageLink<Void> link = new BookmarkablePageLink<>("buy", BuyPage.class, pageParameters);
                    link.add(new Label("buy", "Buy"));
                    item.add(link);
                }
            };
            add(listview);
        }
    }
}
